from django.db import models


class TbWorkflowRole2(models.Model):
    username = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'tb_workflow_role2'


class Duty(models.Model):
    name = models.CharField(max_length=50)
    code = models.CharField(max_length=50, blank=True, null=True)
    desc = models.CharField(max_length=500, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'tb_duty'


class OrgLevel(models.Model):
    level = models.IntegerField(primary_key=True)
    level_name = models.CharField(max_length=100)
    level_code = models.CharField(max_length=50)
    duty = models.ForeignKey(Duty)
    #id = models.AutoField(unique=True)

    class Meta:
        db_table = 'tb_org_level'


class Org(models.Model):
    org_name = models.CharField(max_length=100)
    parent_id = models.IntegerField(blank=True, null=True)
    create_date = models.DateTimeField()
    org_code = models.CharField(max_length=50, blank=True, null=True)
    cost_code = models.CharField(max_length=50, blank=True, null=True)
    org_desc = models.CharField(max_length=500, blank=True, null=True)
    org_desc2 = models.CharField(max_length=500, blank=True, null=True)
    org_level = models.ForeignKey(OrgLevel)
    is_active = models.IntegerField()

    class Meta:
        db_table = 'tb_org'

class Status(models.Model):
    status_code = models.IntegerField(primary_key=True)
    status_name = models.CharField(max_length=100)
    status_desc = models.CharField(max_length=100)
    id = models.IntegerField()


class User(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=30)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()
    nfc_id = models.CharField(db_column='NFC_id', max_length=100, blank=True, null=True)  # Field name made lowercase.
    ext = models.CharField(max_length=50, blank=True, null=True)
    short_number = models.CharField(max_length=50, blank=True, null=True)
    mobile_phone = models.CharField(max_length=50, blank=True, null=True)
    head = models.CharField(max_length=100, blank=True, null=True)
    sign = models.CharField(max_length=100, blank=True, null=True)
    status = models.ForeignKey(Status)
    class Meta:
        db_table = 'tb_user'

    def __str__(self):
        return self.last_name



class OrgRole(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=50)
    desc = models.CharField(max_length=500)
    create_date = models.DateTimeField()

    class Meta:
        db_table = 'tb_org_role'

class UserOrg(models.Model):
    user = models.ForeignKey(User)
    org = models.ForeignKey(Org)
    create_date = models.DateTimeField()
    org_role = models.ForeignKey(OrgRole)

    class Meta:
        db_table = 'tb_user_org'






class Workflow(models.Model):
    name = models.CharField(max_length=20)
    description = models.CharField(max_length=150, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    effective_time = models.IntegerField(blank=True, null=True)
    end = models.IntegerField(blank=True, null=True)
    start = models.IntegerField(blank=True, null=True)
    enabled = models.IntegerField(blank=True, null=True)
    founder = models.CharField(max_length=30, blank=True, null=True)
    rectime = models.DateTimeField(blank=True, null=True)
    flowchart = models.CharField(max_length=20)
    update_time = models.DateTimeField(blank=True, null=True)
    workflow_type = models.CharField(max_length=50)
    update_type = models.CharField(max_length=10)

    class Meta:

        db_table = 'tb_workflow'


class WfsAgent(models.Model):
    id = models.IntegerField(primary_key=True)
    user = models.CharField(max_length=20)
    username = models.CharField(max_length=20)
    org = models.ForeignKey(Org)
    agent = models.ForeignKey(User)
    rectime = models.DateTimeField()
    update = models.DateTimeField()
    enabled = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'tb_wfs_agent'


class WorkflowAgent(models.Model):
    user_agent = models.ForeignKey(WfsAgent)
    rectime = models.DateTimeField()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    status = models.IntegerField()
    action = models.IntegerField()
    form_list = models.TextField()

    class Meta:
        db_table = 'tb_workflow_agent'




class ReminderLog(models.Model):
    no = models.CharField(max_length=50)
    rectime = models.DateTimeField()
    applicant = models.CharField(max_length=50)
    user = models.CharField(max_length=100)
    name = models.CharField(max_length=50)
    type = models.CharField(max_length=20)
    title = models.CharField(max_length=50)
    url = models.CharField(max_length=50)
    status = models.IntegerField()
    num = models.IntegerField(default=0)

    class Meta:

        db_table = 'tb_workflow_reminder_log'


class WorkflowRole(models.Model):
    role = models.CharField(max_length=30)
    desc = models.CharField(max_length=100)
    type = models.CharField(max_length=20)

    def __str__(self):
        return self.role

    class Meta:
        unique_together = (("role","desc"),)
        db_table = 'tb_workflow_role'


class WorkflowRoleUser(models.Model):
    user_id = models.IntegerField()
    workflow_role = models.ForeignKey(WorkflowRole, related_name='user_2')

    class Meta:

        db_table = 'tb_workflow_role_user'


class Workflownode(models.Model):
    name = models.CharField(max_length=45)
    order_number = models.IntegerField(blank=True, null=True)
    property = models.TextField()
    workflow = models.ForeignKey(Workflow)

    class Meta:

        db_table = 'tb_workflownode'


class Workflowrecord(models.Model):
    no = models.CharField(db_column='NO', max_length=50, blank=True, null=True)
    pdfmode = models.CharField(max_length=200)  # Field name made lowercase.
    desc = models.CharField(max_length=200, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    applicant = models.CharField(max_length=45, blank=True, null=True)
    status = models.CharField(max_length=45, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    workflow = models.ForeignKey(Workflow)
    pdf = models.CharField(max_length=200)
    #pdf3 = models.CharField(max_length=200)
    end_time = models.CharField(max_length=45, blank=True, null=True)
    org_id = models.IntegerField()
    signature_graph = models.IntegerField()

    class Meta:
        db_table = 'tb_workflowrecord'

class ForwardBackMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:

        db_table = 'tb_forward_back_mode'


class ForwardMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:

        db_table = 'tb_forward_mode'


class NodeGroup(models.Model):
    workflowrecord = models.ForeignKey(Workflowrecord)
    name = models.CharField(max_length=45)
    order_number = models.IntegerField()

    class Meta:

        db_table = 'tb_node_group'


class RejectMode(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=50)

    class Meta:

        db_table = 'tb_reject_mode'


class UserMode(models.Model):
    name = models.CharField(max_length=50)

    class Meta:

        db_table = 'tb_user_mode'

class NodeCase(models.Model):
    group = models.ForeignKey(NodeGroup, related_name='group')
    order_number = models.IntegerField(blank=True, null=True)
    review_result = models.CharField(max_length=100)
    approver = models.CharField(max_length=45, blank=True, null=True)
    rectime = models.DateTimeField(blank=True, null=True)
    username = models.CharField(max_length=45)
    user_choose = models.CharField(max_length=100, blank=True, null=True)
    status = models.CharField(max_length=10, blank=True, null=True)
    user_mode = models.CharField(max_length=25,db_column='user_mode', blank=True, null=True)
    forward_mode = models.CharField(max_length=25,db_column='forward_mode', blank=True, null=True)
    forward_back_mode = models.CharField(max_length=25,db_column='forward_back_mode', blank=True, null=True)
    reject_mode = models.CharField(max_length=25,db_column='reject_mode', blank=True, null=True)
    forward_list = models.CharField(max_length=100, blank=True, null=True)
    user_role_list = models.CharField(max_length=100, blank=True, null=True)
    agent = models.CharField(max_length=45, blank=True, null=True)

    class Meta:
        db_table = 'tb_node_case'

    def count(self):
        """
        Performs a SELECT COUNT() and returns the number of records as an
        integer.

        If the QuerySet is already fully cached this simply returns the length
        of the cached results set to avoid multiple SELECT COUNT(*) calls.
        """
        if self._result_cache is not None and not self._iter:
            return len(self._result_cache)
        return self.query.get_count(using=self.db)




class WorkflowApprovalRecord(models.Model):
    workflow_record = models.ForeignKey(Workflowrecord)
    status = models.CharField(max_length=20, blank=True, null=True)
    approver = models.CharField(max_length=20, blank=True, null=True)
    username = models.CharField(max_length=45, blank=True, null=True)
    next = models.CharField(max_length=20, blank=True, null=True)
    start_time = models.DateTimeField(blank=True, null=True)
    end_time = models.DateTimeField(blank=True, null=True)
    order_number = models.IntegerField(blank=True, null=True)
    action = models.CharField(max_length=20, blank=True, null=True)
    next_approver = models.CharField(max_length=100)
    desc = models.CharField(max_length=200)
    signature = models.CharField(max_length=200)
    filename =  models.CharField(max_length=200)
    pdf = models.CharField(max_length=200)
    #this = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        db_table = 'tb_workflow_approval_record'

    def count(self):
        """
        Performs a SELECT COUNT() and returns the number of records as an
        integer.

        If the QuerySet is already fully cached this simply returns the length
        of the cached results set to avoid multiple SELECT COUNT(*) calls.
        """
        if self._result_cache is not None and not self._iter:
            return len(self._result_cache)
        return self.query.get_count(using=self.db)


import json
class WFSOrgRole(models.Model):
    user = models.ForeignKey(User)
    role = models.ForeignKey(WorkflowRole, related_name='user2')
    rectime = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.user.last_name+'('+self.user.username+')'

    class Meta:
        db_table = 'tb_wfs_org_role'

class AgentUserView(models.Model):
    user = models.CharField(max_length=20)
    org_id = models.IntegerField()
    agent = models.CharField(max_length=20)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

    class Meta:
        db_table = 'agent_user_view'

class Approver(models.Model):
    name = models.CharField(max_length=50)
    approver_record = models.CharField(max_length=50)
    time = models.FloatField()

    class Meta:
        db_table = 'approver_record_view'

class ByFromApprover(models.Model):
    typeid = models.IntegerField()
    typename = models.CharField(max_length=50)
    name = models.CharField(max_length=50)
    approver_record = models.CharField(max_length=20)
    time = models.FloatField()
    endtime = models.DateTimeField()
    starttime = models.DateTimeField()

    class Meta:
        db_table = 'by_type_approver'


class ByFromApprover2(models.Model):
    typeid = models.IntegerField()
    typename = models.CharField(max_length=50)
    name = models.CharField(max_length=50)
    approver_record = models.CharField(max_length=20)
    time = models.FloatField()
    endtime = models.DateTimeField()
    starttime = models.DateTimeField()

    class Meta:
        db_table = 'by_type_approver_aaaaaa_view'

class ByFromType(models.Model):
    name = models.CharField(max_length=50)
    types = models.IntegerField()
    endtime = models.DateTimeField()
    starttime = models.DateTimeField()
    time = models.FloatField()

    class Meta:
        db_table = 'by_type'

class ByNode(models.Model):
    next_name = models.CharField(max_length=50,db_column='next')
    time = models.FloatField(db_column='num')
    approver_record = models.CharField(max_length=20)
    name = models.CharField(max_length=50)

    class Meta:
        db_table = 'by_node_view'


class ByFromTypeMonth(models.Model):
    type_name = models.CharField(max_length=50,db_column='type')
    type_id = models.IntegerField()
    time = models.FloatField()
    date = models.DateField()

    class Meta:
        db_table = 'by_from_type_month_view'